package com.example.cafe.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.cafe.work.dto.OrderDisplay;
import com.example.cafe.work.dto.ResResult;
import com.example.cafe.work.dto.Result;
import com.example.cafe.work.mapper.OrderMapper;
import com.example.cafe.work.model.Coffee;
import com.example.cafe.work.model.Order;
import com.example.cafe.work.model.Userinfo;
import com.example.cafe.work.mapper.UserinfoMapper;
import com.example.cafe.work.service.UserinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cafe.work.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.example.cafe.work.model.StateEnum.PENDING_PAYMENT;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hua
 * @since 2024-09-16
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements UserinfoService {
    @Resource
    OrderMapper orderMapper;

    @Transactional
    @Override
    public Result Recharge(String userId, int money) {
        boolean update = update(new UpdateWrapper<Userinfo>()
                .eq("user_id", userId)
                .setSql("money=money+" + money));
        if (update) {
            return Result.ok();
        } else throw new RuntimeException("充值失败");
    }

    @Override
    public Result Consume(List<Order> orders) {
        ArrayList<String> list = new ArrayList<>();
        //从threadLocal中拿到当前用户的userid
        String userId = UserHolder.getUser().getUserId();
        int total = 0;
        for (Order order : orders) {
            //将id添加到新的list中，并计算传入的订单组的总价
            list.add(order.getOrderId());
            total = total + order.getCofTotal();
        }
        //根据传的userId查询用户的余额
        Integer left = query().eq("user_id", userId).one().getMoney();
        if (left < total) {
            throw new RuntimeException("余额不足，请充值");
        }
        //操作数据的修改
        boolean update = update(new UpdateWrapper<Userinfo>()
                .eq("user_id", userId)
                .setSql("money=money-" + total));
        int consume = 0;
        if (update) {
            //支付完成之后，对订单进行状态的修改
            consume = orderMapper.consume(list);
        }
        if (consume == orders.size()) {
            return Result.ok("支付成功");
        } else throw new RuntimeException("支付失败");

    }

    @Override
    public Result ChargeBack(List<OrderDisplay> orders) {
        ArrayList<String> notPay = new ArrayList<>();
        ArrayList<String> making = new ArrayList<>();
        int total = 0;
        //前端筛选出未支付或者制作中
        for (OrderDisplay order : orders) {
            switch (order.getStateName()){
                case "待支付":
                    notPay.add(order.getOrderId());
                    break;
                case "制作中":
                    making.add(order.getOrderId());
                    total = total + order.getCofTotal();
                    break;
                case "已完成":
                    throw new RuntimeException("已完成的退不了");
            }
        }
        System.out.println(notPay);
        System.out.println(making);
        //对于待支付的，直接修改状态,传入list和其对应状态
        int pay = orderMapper.ChargeBack(notPay);
        //校验是否全部修改
        if (!(pay ==notPay.size())){throw new RuntimeException("退单异常");}
        //对于已支付即制作中的，修改状态的同时，将订单金额返回账户
        int mak = orderMapper.ChargeBack(making);
        //校验
        if (!(mak== making.size())){throw new RuntimeException("退单异常");}
        //校验金额是否全部返回账户
        Result recharge = Recharge(UserHolder.getUser().getUserId(), total);
        if (recharge.getSuccess()){
            return Result.ok();
        }else throw new RuntimeException("退单异常，金额返回出现问题");
    }
}
